var roomId = 1;
$(document).ready(function() {
 	$.getJSON(ctx + "/room/dataGrid",'',function(data){
		var str = "";
		for ( var i = 0; i < data.rows.length; i++) {
			str += "<div class='external-event rooms' id=" + data.rows[i].id + ">" + data.rows[i].name + "</div>";
		}
		$("#room_name_div").append(str);
		$(".external-event").click(function(){
			$(".external-event").css("border-style","solid ");
			$(this).css("border-style","solid dotted dashed double");
			roomId = this.id;
			calendar.fullCalendar('refetchEvents');
		});
	});
		var date = new Date();
		var d = date.getDate();
		var m = date.getMonth();
		var y = date.getFullYear();
		
		var calendar = $('#calendar').fullCalendar({
			header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,agendaWeek,agendaDay'
			},
			lazyFetching : false, 
			defaultView:'agendaWeek',
			firstDay:1,
			allDaySlot: false,
			allDayDefault: false,
			buttonText: {
				today: '今天',
				month: '月',
				week: '周',
				day: '日'
			},
			monthNames: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
			monthNamesShort: ['1','2','3','4','5','6','7','8','9','10','11','12'],
			dayNames: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
			dayNamesShort: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'],
			timeFormat: {
				agenda: 'h:mmtt{ - h:mmtt}',
				'': 'h(:mm)tt'
			},
			columnFormat: {
				month: 'ddd',
				week: 'MM/dd ddd',
				day: 'ddd'
			},
			titleFormat: {
				month: 'yyyy 年 MMM 月',
				week: "yyyy.MM.dd{ '&#8212;' yyyy.MM.dd}",
				day: 'ddd'
			},
			snapMinutes : 5,
			slotEventOverlap : false,
			selectable: true,
			selectHelper: true,
			select: function(start, end, allDay) {
				if(start.getTime() <= new Date().getTime()){
					alert("开始时间已过期!");
				}else{
					var title = prompt('会议主题:');
					if (title) {
						$.post(ctx + "/roomSchedule/addRoomEvent",{start: start.getTime(), end: end.getTime(), roomId: roomId, title: title},
						function(data){
							var obj = jQuery.parseJSON(data);
							if(obj.success){
//								calendar.fullCalendar('renderEvent',{title: title,start: start,end: end,allDay: allDay},true);
								$('#calendar').fullCalendar('refetchEvents'); 
							}
						})
/*						 $.ajax({  
			                   type: "POST",  
			                   url: ctx + "/roomSchedule/addRoomEvent",  
			                   data: {start: start.getTime(), end: end.getTime(), roomId: roomId, title: title},  
			                   success:function(data){  
			                	   if(data.success){
			                		   alert(11);
			                		   calendar.fullCalendar('renderEvent',{title: title,start: start,end: end,allDay: allDay},true);
									}  
			                   }  
			            });*/
					}
				}
/*				calendar.fullCalendar('renderEvent',
						{
							title: title,
							start: start,
							end: end,
							allDay: allDay
						},
						true // make the event "stick"
					);*/
				$('#calendar').fullCalendar('unselect');
			},
			eventDrop: function( event, dayDelta, minuteDelta, allDay, revertFunc, jsEvent, ui, view) {
				if(event.start.getTime() <= new Date().getTime()){
					alert("开始时间已过期!");
					revertFunc();
				}else{
					$.post(ctx + "/roomSchedule/updateRoomEvent",{id: event.id,start: event.start.getTime(), end: event.end.getTime()}, function(data){
						var obj = jQuery.parseJSON(data);
						if(!obj.success){
							alert(data.msg);
							revertFunc();
						}
						$('#calendar').fullCalendar('refetchEvents'); 
					});
				} 
			},
			 eventClick: function(calEvent, jsEvent, view) {
				if (confirm('删除该事件？')) {
					calendar.fullCalendar('removeEvents',calEvent.id);
					$.post(ctx + "/roomSchedule/deleteRoomEvent",{id: calEvent.id}, function(data){
						var obj = jQuery.parseJSON(data);
						if(!obj.success){
							alert(data.msg);
							revertFunc();
						}
						$('#calendar').fullCalendar('refetchEvents'); 
					});
				}
		        // change the border color just for fun
		        $(this).css('border-color', 'red');

		    },
			editable: true,
			events: function( start, end, callback ) {
				$.post(ctx + "/roomSchedule/dataGrid", {roomid: roomId,start:Math.round(start.getTime()/1000), end:Math.round(end.getTime()/1000)}, function(events){
					var objs = jQuery.parseJSON(events);
					console.info(objs.obj)
					callback(objs.obj);
				});
			},
			
			eventResize :function( event, jsEvent, ui, view,revertFunc ) { 
				if(event.start.getTime() <= new Date().getTime()){
					alert("开始时间已过期!");
					revertFunc();
				}else{
					$.post(ctx + "/roomSchedule/updateRoomEvent",{id: event.id, start: event.start.getTime(), end: (event.end.getTime()) }, function(data){
						var obj = jQuery.parseJSON(data);
						if(!obj.success){
							alert(data.msg);
							revertFunc();
						}
						$('#calendar').fullCalendar('refetchEvents'); 
					});
				}
			},
		});
										
});


function changeRoom(id) {
	$(".external-event").each(function(i) {
		roomId = id;
	});
	$('#calendar').fullCalendar('refetchEvents');
}
function getTipContent(initiator, start, end, subject, contact) {
	$("#initiator").text(initiator);
	$("#contact").text(contact);
	$("#tb").text(
			$.fullCalendar.formatDate(start, 'hh:mmtt') + " - "
					+ $.fullCalendar.formatDate(end, 'hh:mmtt'));
	$("#subject").text(subject);
	return $('#qTipContent');
}